package com.demo.boss.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.boss.common.convert.ProductConvert;
import com.demo.boss.common.enums.SysCode;
import com.demo.boss.entity.PayProduct;
import com.demo.boss.models.PageRequest;
import com.demo.boss.models.Result;
import com.demo.boss.models.request.ProductRequest;
import com.demo.boss.models.vo.DicItemVo;
import com.demo.boss.server.ProductService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class ProductController {
    @Autowired
    private ProductService productService;
    @Resource
    private ProductConvert productConvert;

    /**
     * 分页查询产品信息
     */
    @GetMapping("/product/pageList")
    public Result pageList(PageRequest pageRequest) {
        IPage<PayProduct> payProductIPage = productService.pageList(pageRequest);
        Map<String, Object> data = new HashMap<>();
        data.put("current", payProductIPage.getCurrent());
        data.put("pages", payProductIPage.getPages());
        data.put("list", productConvert.convertListPayProductVo(payProductIPage.getRecords()));
        return Result.ok(data);
    }

    /**
     * 添加产品信息
     */
    @PostMapping("/product/add")
    public Result productAdd(@RequestBody ProductRequest productRequest) {
        productService.productAdd(productRequest);
        return Result.ok();
    }

    /**
     * 字典数据
     */
    @GetMapping("/product/dic")
    public Result queryProductDic(@Validated PageRequest pageRequest) {
        List<DicItemVo> dicItemVoList = productService.queryProductDic(pageRequest);
        return Result.ok(dicItemVoList);

    }

    /**
     * 产品模糊查询接口
     */
    @GetMapping("/product/match")
    public Result queryProductName(String productName) {
        if (StrUtil.isNotBlank(productName) && productName.trim().length() > 1) {
            List<PayProduct> productList = productService.matchProductName(productName);
            return Result.ok(productList);
        }
        return Result.fail(SysCode.REQUEST_PARAM_ERROR);
    }
}
